package com.wwtech.epp.view.dynamicapp;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.wwtech.epp.R;
import com.wwtech.epp.contain.ContainValue;
import com.wwtech.epp.db.Friend;
import com.wwtech.epp.entity.DynamicUI;
import com.wwtech.epp.ui.activity.friend.AddPersonActivity;
import com.wwtech.epp.ui.activity.work.NewProjectActivity;
import com.wwtech.epp.ui.adapter.work.ProjectAdapter;
import com.wwtech.epp.view.ProjectMemberView;

import java.util.ArrayList;

/**
 * Created by hu on 2017/9/18.
 * 联系人
 */

public class ContactsAppView extends LinearLayout {

    private TextView title;
    private RecyclerView recyclerView;
    private DynamicUI.DataBean dataBean;
    private String key;
    private Friend tempFriend;

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public ArrayList<Friend> getPersonList() {
        return personList;
    }

    public void setPersonList(ArrayList<Friend> personList) {
        this.personList = personList;
    }

    private ArrayList<Friend> personList;

    public TextView getTitle() {
        return title;
    }

    public void setTitle(TextView title) {
        this.title = title;
    }

    public RecyclerView getRecyclerView() {
        return recyclerView;
    }

    public void setRecyclerView(RecyclerView recyclerView) {
        this.recyclerView = recyclerView;
    }

    public DynamicUI.DataBean getDataBean() {
        return dataBean;
    }

    public void setDataBean(DynamicUI.DataBean dataBean) {
        this.dataBean = dataBean;
        initContent();
    }

    public ProjectAdapter getPartMamberAdapter() {
        return partMamberAdapter;
    }

    public void setPartMamberAdapter(ProjectAdapter partMamberAdapter) {
        this.partMamberAdapter = partMamberAdapter;
    }

    private ProjectAdapter partMamberAdapter;      //参与人的adapter

    public ContactsAppView(Context context) {
        super(context);
        init(context);
    }

    private void init(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.appview_contacts, this, true);
        title = (TextView) view.findViewById(R.id.title);
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
    }

    private void initContent() {
        personList = new ArrayList<>();
        title.setText(dataBean.getTitle());
        tempFriend = new Friend("", "-1", "", Uri.parse(""));
        tempFriend.setUserId("-1");
        personList.add(tempFriend);
        partMamberAdapter = new ProjectAdapter(personList);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 5);
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.setAdapter(partMamberAdapter);
        recyclerView.setNestedScrollingEnabled(false);
        partMamberAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                ProjectMemberView projectMemberView = (ProjectMemberView) view;
                if (projectMemberView.isSetInfo()) {
                    personList.remove(i);
                    partMamberAdapter.notifyDataSetChanged();
                } else {
                    personList = (ArrayList<Friend>) partMamberAdapter.getData();
                    Intent intent = new Intent(getContext(), AddPersonActivity.class);
                    intent.putExtra("title", "选择" + dataBean.getTitle());
                    intent.putExtra("key", key);
                    ContainValue.partPerson = personList;
                    ((Activity)getContext()).startActivityForResult(intent, NewProjectActivity.CHECKPART);
                }
            }
        });
    }

    public void notifyDataChange() {
        partMamberAdapter.notifyDataSetChanged();
    }
}
